package io.github.wslxm.springbootplus2.manage.sys.controller;


import io.github.wslxm.springbootplus2.core.base.controller.BaseController;
import io.github.wslxm.springbootplus2.core.base.model.BasePage;
import io.github.wslxm.springbootplus2.core.constant.BaseConstant;
import io.github.wslxm.springbootplus2.core.result.Result;
import io.github.wslxm.springbootplus2.manage.sys.model.dto.SysMenuDTO;
import io.github.wslxm.springbootplus2.manage.sys.model.query.SysMenuQuery;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysMenuVO;
import io.github.wslxm.springbootplus2.manage.sys.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *   菜单
 *
 * @author 王松
 * @WX-QQ 1720696548
 * @date 2019/11/13 13:38
 */
@RestController
@RequestMapping(BaseConstant.Uri.API_ADMIN+ "/sys/menu")
@Tag(name = "base--sys--菜单管理")
public class SysMenuController extends BaseController<SysMenuService> {


    @GetMapping(value = "/tree")
    @Operation(summary = "列表查询(不支持分页)", description = "根据sort正序排序返回")
    public Result<List<SysMenuVO>> tree(@ModelAttribute SysMenuQuery query) {
        return Result.success(baseService.tree(query));
    }

    @GetMapping(value = "/findPage")
    @Operation(summary = "列表查询")
    public Result<BasePage<SysMenuVO>> findPage(@ModelAttribute @Validated SysMenuQuery query) {
        return Result.success(baseService.findPage(query));
    }

    @GetMapping(value = "/{id}")
    @Operation(summary = "ID查询")
    public Result<SysMenuVO> findId(@PathVariable String id) {
        return Result.success(baseService.findId(id));
    }

    @PostMapping
    @Operation(summary = "菜单添加")
    public Result<String> insert(@RequestBody SysMenuDTO dto) {
        return Result.successInsert(baseService.insert(dto));
    }


    @PutMapping(value = "/{id}")
    @Operation(summary = "ID编辑", description = "")
    public Result<Boolean> upd(@PathVariable String id, @RequestBody SysMenuDTO dto) {
        return Result.successUpdate(baseService.upd(id, dto));
    }


    @PutMapping(value = "updPid/{id}")
    @Operation(summary = "修改父级", description = "")
    public Result<Boolean> updPid(@PathVariable String id, @RequestParam  String pid) {
        return Result.successUpdate(baseService.updPid(id, pid));
    }


    @DeleteMapping(value = "/{id}")
    @Operation(summary = "ID删除", description = "同时删除当前菜单和当前菜单下的所有子菜单")
    public Result<List<String>> del(@PathVariable String id) {
        return Result.successDelete(baseService.del(id));
    }


    @GetMapping(value = "/findTree")
    @Operation(summary = "左导航菜单", description = "当前用户对应的角色菜单数据, 树结构数据,无限级,不限制层次,根据sort字段正序排序,sort越小越靠前")
    public Result<List<SysMenuVO>> findTree() {
        return Result.successFind(baseService.findTree());
    }
}
